﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using BCCommon;
using BCEntity.WeChatComponent.WeChatMessagePushResultVerificationRecord;
using Dapper;

namespace BCData.WeChatComponent.WeChatMessagePushResultVerificationRecord
{
    /// <summary>
    /// 微信群发结果检验记录数据处理
    /// </summary>
    public class WeChatMessagePushResultVerificationRecordData : IWeChatMessagePushResultVerificationRecordData
    {
        private readonly IDatabaseContext databaseContext;
        private const string QUERY = @"SELECT WeChatMessagePushResultVerificationRecordId,MsgId,
                                            MediaId,WeChatNewsMaterialArticleRecordId,`Index`,UserDeclareState,AuditState,
                                            OriginalArticleUrl,OriginalArticleType,CanReprint,NeedReplaceContent,NeedShowReprintSource
                                        FROM WeChatMessagePushResultVerificationRecord";
        public WeChatMessagePushResultVerificationRecordData(IDatabaseContext databaseContext)
        {
            this.databaseContext=databaseContext;
        }

        public void Add(WeChatMessagePushResultVerificationRecordEntity entity)
        {
            string sql = @"INSERT INTO WeChatMessagePushResultVerificationRecord(MsgId,
                                    MediaId,WeChatNewsMaterialArticleRecordId,`Index`,UserDeclareState,AuditState,
                                    OriginalArticleUrl,OriginalArticleType,CanReprint,NeedReplaceContent,NeedShowReprintSource)
                                VALUES(?MsgId,?MediaId,?WeChatNewsMaterialArticleRecordId,?Index,?UserDeclareState,
                                    ?AuditState,?OriginalArticleUrl,?OriginalArticleType,?CanReprint,?NeedReplaceContent,?NeedShowReprintSource)";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?MsgId", entity.MsgId, DbType.Int64, size: 20);
            parameters.Add("?MediaId", entity.MediaId, DbType.AnsiString, size: 100);
            parameters.Add("?WeChatNewsMaterialArticleRecordId", entity.WeChatNewsMaterialArticleRecordId, DbType.AnsiString, size: 36);
            parameters.Add("?Index", entity.Index, DbType.Int32, size: 10);
            parameters.Add("?UserDeclareState", entity.UserDeclareState, DbType.Int32, size: 10);
            parameters.Add("?AuditState", entity.AuditState, DbType.Int32, size: 10);
            parameters.Add("?OriginalArticleUrl", entity.OriginalArticleUrl, DbType.AnsiString, size: 1000);
            parameters.Add("?OriginalArticleType", entity.OriginalArticleType, DbType.Int32, size: 10);
            parameters.Add("?CanReprint", entity.CanReprint, DbType.Int32, size: 10);
            parameters.Add("?NeedReplaceContent", entity.NeedReplaceContent, DbType.Int32, size: 10);
            parameters.Add("?NeedShowReprintSource", entity.NeedShowReprintSource, DbType.Int32, size: 10);
            this.databaseContext.Master.Execute(sql, parameters, databaseContext.Transaction);
        }

        public IEnumerable<WeChatMessagePushResultVerificationRecordEntity> Get(long? msgId, string mediaId)
        {
            DynamicParameters parameters = new DynamicParameters();
            List<string> wheres = new List<string>();
            if (msgId.HasValue)
            {
                wheres.Add("MsgId=?MsgId");
                parameters.Add("?MsgId", msgId.Value ,DbType.UInt64, size: 20);
            }
            if (!string.IsNullOrWhiteSpace(mediaId))
            {
                wheres.Add("MediaId=?MediaId");
                parameters.Add("?MediaId", mediaId, DbType.AnsiString, size: 100);
            }
            string sql = string.Format(@"{0} {1} ORDER BY `Index` ASC", QUERY, wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "");
            return this.databaseContext.Slave.Query<WeChatMessagePushResultVerificationRecordEntity>(sql, parameters);
        }
    }
}
